package com.darren.archive.controller;

import com.darren.archive.entity.SysRole;
import com.darren.archive.service.SysRoleService;
import com.darren.framework.bean.ResponseBean;
import com.darren.framework.controller.BaseController;
import com.darren.framework.utils.Constant;
import com.darren.framework.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(value = "系统角色", tags = "系统角色接口", description = "系统角色接口")
@Controller
public class SysRoleController extends BaseController {

    private Logger logger = LogManager.getLogger(SysRoleController.class);

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 根据角色名称模糊查找角色
     *
     * @param name     角色名称
     * @param pageNum  页号
     * @param pageSize 页大小
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页号", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer")
    })
    @ApiOperation(value = "查询角色", notes = "根据角色名称模糊查找角色")
    @ResponseBody
    @RequiresPermissions("role:view")
    @GetMapping(value = "/sys/role/list")
    public ResponseBean getList(@RequestParam(name = "name", required = false) String name,
                                @RequestParam(name = "pageNum", required = false, defaultValue = "0") Integer pageNum,
                                @RequestParam(name = "pageSize", required = false, defaultValue = Constant.PAGE_SIZE_STRING) Integer pageSize) {
        Sort sort = new Sort(Sort.Direction.ASC, "displaySequence");
        Pageable pageable = PageRequest.of(pageNum, pageSize,sort);
        if (StringUtils.isEmpty(name)) {
            return ResponseBean.isOk(sysRoleService.findAll(pageable));
        }
        return ResponseBean.isOk(sysRoleService.findByNameLike("%" + name + "%", pageable));
    }


    /**
     * 保存角色信息
     *
     * @param record 角色信息
     * @return
     */
    @ResponseBody
    @RequiresPermissions("role:edit")
    @ApiOperation(value = "保存角色信息", notes = "保存角色信息 SysRole，如果 id 为空，则新增；id 不为空，则修改")
    @PostMapping(value = "/sys/role/save", consumes = "application/json;charset=UTF-8")
    public ResponseBean save(@RequestBody @ApiParam(name = "保存角色信息", value = "传入json格式", required = true) SysRole record) {
        return ResponseBean.isOk(sysRoleService.save(record));
    }


    /**
     * 根据 id 删除 角色信息
     *
     * @param id 角色ID
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Long")
    })
    @ResponseBody
    @RequiresPermissions("role:delete")
    @ApiOperation(value = "根据id 删除 角色信息", notes = "根据id 删除 角色信息")
    @DeleteMapping(value = "/sys/role/{id}")
    public ResponseBean deleteById(@PathVariable(name = "id") Long id) {
        sysRoleService.deleteById(id);
        return ResponseBean.isOk();
    }


    /**
     * 根据id 查询 角色信息
     *
     * @param id 角色Id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Long")
    })
    @ResponseBody
    @RequiresPermissions("role:view")
    @ApiOperation(value = "根据id 查询 角色信息", notes = "根据id 查询 角色信息")
    @GetMapping(value = "/sys/role/{id}")
    public ResponseBean getById(@PathVariable(name = "id") Long id) {
        return ResponseBean.isOk(sysRoleService.findById(id));
    }


    /**
     * 保存是否启用状态
     *
     * @param record 角色信息
     * @return
     */
    @ResponseBody
    @RequiresPermissions("role:edit")
    @ApiOperation(value = "保存是否启用状态", notes = "保存是否启用状态")
    @PostMapping(value = "/sys/role/enable/save", consumes = "application/json;charset=UTF-8")
    public ResponseBean saveEnable(@RequestBody @ApiParam(name = "角色信息", value = "传入json格式", required = true) SysRole record) {
        return ResponseBean.isOk(sysRoleService.saveEnable(record));
    }

}
